#!/bin/bash
##卸载相关服务
if [[ "$1" == "1" ]];then
	gl="yum"
elif [[ "$1" == "2" ]];then
	gl="apt-get"
else
	echo -e "请选择包管理器\n[1]yum\n[2]apt\n默认使用yum"
	read gls
	if [[ "$gls" == "2" ]];then
		gl="apt-get"
	else
		gl="yum"
	fi
fi
start_unstaller_docker(){
	sudo docker rm -f $(docker ps -aq)
	sudo systemctl stop docker
	sudo $gl remove -y docker* contai*
	sudo rm -rf /etc/systemd/system/docker.service.d
	sudo rm -rf /var/lib/docker/*
	if [[ "$?" == "0" ]];then
		echo '卸载完成'
	else
		echo -e "/var/lib/docker/\n目录删除失败"
	fi
}
start_unstall_k8s(){
	sudo docker rm -f $(sudo docker ps -qa)
	sudo $gl remove -y kubelet kubeadm kubectl	
	sudo rm -rf /var/etcd	
	for m in $(sudo tac /proc/mounts | sudo awk '{print $2}'|sudo grep /var/lib/kubelet);do
		sudo umount $m||true
	done
	sudo rm -rf /var/lib/kubelet/
	for m in $(sudo tac /proc/mounts | sudo awk '{print $2}'|sudo grep /var/lib/rancher);do
		 sudo umount $m||true
	done
	sudo rm -rf /var/lib/rancher/
	sudo rm -rf /run/kubernetes/
	sudo docker volume rm $(sudo docker volume ls -q)
	sudo docker ps -a
	echo '剩余容器列表 按回车继续查看剩余卷列表'
	read y
	sudo docker volume ls
	echo '剩余docker卷列表'
}
unstaller_service(){
	s=`rpm -qa | grep docker`
	if [[ -z "$s" ]];then
		echo -e "本机未安装docker\n是否继续卸载其他组件及残留文件[y/n]\n默认N"
		read p
		if  [[ "$p" == "y" || "$p" == "Y" ]];then
			start_unstaller_docker
		else
			echo '跳过卸载docker'
		fi
	else
		echo -e "是否确定卸载[y/n]\n默认N"
		read p
		if  [[ "$p" == "y" || "$p" == "Y" ]];then
                        start_unstaller_docker
                else
                        echo '跳过卸载docker'
                fi
	fi
}
if [[ "$1" == "1" ]];then
	start_unstall_k8s
	unstaller_service
elif [[ "$1" == "2" ]];then
	start_unstall_k8s
elif [[ "$1" == "3" ]];then
	start_unstaller_docker
else
	echo '选择错误'
fi
